﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDHDeepTunnel.Interfaces;
using DDHDeepTunnel.Data;
using NativeDataOperation.Interfaces;
using WebFoundation;
using WebFoundation.Data;

namespace DDHDeepTunnel.Business
{
    internal class Table : ITable
    {
        private DbManager _queryDbManager;
        private DbManager _writeDbManager;

        public Table(DbManager queryDbManager, DbManager writeDbManager, OnlineDataModule module)
        {
            this._queryDbManager = queryDbManager;
            this._writeDbManager = writeDbManager;
        }

        public string CreateTable(string tableName,
            List<SqlPropertyMap> properties = null)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).CreateTable(tableName, properties);
        }

        public string DropTable(string tableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).DropTable(tableName);
        }

        public string CloneTableStructure(string clonedTableName,
            string afterClonedTableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).CloneTableStructure(clonedTableName, afterClonedTableName);
        }

        public string CloneTableStructureAndData(string clonedTableName,
            string afterClonedTableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).CloneTableStructureAndData(clonedTableName, afterClonedTableName);
        }

        public DataTable GetDataTableRunSql(string sqlString)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).GetDataTableRunSql(sqlString);
        }

        public bool TableExist(string tableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).TableExist(tableName);
        }

        public string TruncateTableDatas(string tableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).TruncateTableDatas(tableName);
        }

        public string DeleteTableDatas(string tableName, string whereParts)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).DeleteTableDatas(tableName, whereParts);
        }

        public int GetExcuteRunSql(string sqlString)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).GetExcuteRunSql(sqlString);
        }
    }
}